package com.duck.user.utils;


//import com.duck.duckpulse.pojo.User.LoginUserPojo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    private final RedisTemplate<String, String> redisTemplate;

    public RedisUtils(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private final ObjectMapper objectMapper = new ObjectMapper();

//    public void setObject(String key, LoginUserPojo user) {
//        String userJson = null;
//        try {
//            userJson = objectMapper.writeValueAsString(user);
//        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
//        }
//        redisTemplate.opsForValue().set(key, userJson);
//    }
//
//    public LoginUserPojo getObject(String key) throws JsonProcessingException {
//        String userJson = String.valueOf(redisTemplate.opsForValue().get(key));
//        return objectMapper.readValue(userJson, LoginUserPojo.class);
//    }

    public void delObject(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 设置值，不包含时间
     **/
    public void setValue(String key, String value) {
        setValue(key, value, null);
    }

    /**
     * 设置值，包括时间
     **/

    public void setValue(String key, String value, Long time) {
        redisTemplate.opsForValue().set(key, value);
        if (time != null) {
            redisTemplate.expire(key, time, TimeUnit.MINUTES);
        }
    }

    /**
     * 根据key进行取值
     **/

    public String getValue(String key) {
        return String.valueOf(redisTemplate.opsForValue().get(key));
    }

    /**
     * 获取时间
     *
     * @param key 请求路径+ip
     * @return long
     */
    public Long getLastUpdateTime(String key) {
        try {
            return Long.valueOf(Objects.requireNonNull(redisTemplate.opsForValue().get(key + "_last_update_time")));
        } catch (NullPointerException e) {
            return System.currentTimeMillis();
        }
    }

    /**
     * 存放时间
     *
     * @param key  请求路径+ip
     * @param time 时间
     */
    public void updateLastUpdateTime(String key, long time) {
        redisTemplate.opsForValue().set(key + "_last_update_time", String.valueOf(time));
    }

    /**
     * 存入当前水量
     *
     * @param key   url+ip
     * @param water 当前水量
     */
    public void setWater(String key, Long water) {
        redisTemplate.opsForValue().set(key, String.valueOf(water));
    }

    /**
     * 获取当前水量
     *
     * @param key url +ip
     * @return long
     */
    public Long getWater(String key) {
        try {
            return Long.valueOf(Objects.requireNonNull(redisTemplate.opsForValue().get(key)));
        } catch (NullPointerException e) {
            return 0L;
        }
    }
}


